package View;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.html.HTMLDocument.Iterator;

import persistence.Track;

import model.Player;

import View.Components.JPanelTouch;
import View.Components.JScrollPaneTouch;
import View.Components.TouchButton;
import View.Components.TouchObjectContainer;

public class PlayList extends JPanel implements TouchObjectContainer,
        ActionListener {
    private static final long serialVersionUID = -8568752791610500597L;
    public MainWindow _unnamed_Window_;
    private ControllPlayer playerControl;
    private TouchButton leftButt, rightButt;
    private JPanelTouch panelP;
    private JScrollPaneTouch scrollP;
    private Player player;

    public PlayList() {
        super();
        this.setBackground(Color.red);
        this.setLayout(new BorderLayout());
        leftButt = new TouchButton("<");
        rightButt = new TouchButton(">");
        panelP = new JPanelTouch();
        scrollP = new JScrollPaneTouch(panelP);
        panelP.setBackground(new Color(60, 60, 60));
        scrollP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        this.add(leftButt, BorderLayout.WEST);
        this.add(scrollP, BorderLayout.CENTER);
        this.add(rightButt, BorderLayout.EAST);
        leftButt.addActionListener(this);
        rightButt.addActionListener(this);
        this.getPanelP().setLayout(new FlowLayout(FlowLayout.LEFT));
    }

    public void checkForButt() {
        if (this.scrollP.getHorizontalScrollBar().isShowing()) {
            leftButt.setEnabled(true);
            rightButt.setEnabled(true);
        } else {
            leftButt.setEnabled(false);
            rightButt.setEnabled(false);
        }
    }

    /**
     * This method returns the variable playerControl.
     * 
     * @return the playerControl
     */
    public ControllPlayer getPlayerControl() {
        return playerControl;
    }

    /**
     * This method sets the variable playerControl.
     * 
     * @param playerControl
     *            the playerControl to set
     */
    public void setPlayerControl(ControllPlayer playerControl) {
        this.playerControl = playerControl;
    }

    /**
     * This method return the variable leftButt.
     * 
     * @return the leftButt
     */
    public TouchButton getLeftButt() {
        return leftButt;
    }

    /**
     * This methods sets the variable leftButt.
     * 
     * @param leftButt
     *            the leftButt to set
     */
    public void setLeftButt(TouchButton leftButt) {
        this.leftButt = leftButt;
    }

    /**
     * This method return the variable rightButt.
     * 
     * @return the rightButt
     */
    public TouchButton getRightButt() {
        return rightButt;
    }

    /**
     * This methods sets the variable rightButt.
     * 
     * @param rightButt
     *            the rightButt to set
     */
    public void setRightButt(TouchButton rightButt) {
        this.rightButt = rightButt;
    }

    /**
     * This method return the variable panelP.
     * 
     * @return the panelP
     */
    public JPanelTouch getPanelP() {
        return panelP;
    }

    /**
     * This methods sets the variable panelP.
     * 
     * @param panelP
     *            the panelP to set
     */
    public void setPanelP(JPanelTouch panelP) {
        this.panelP = panelP;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        System.out.println(ev.getSource().toString());
        if (ev.getSource().equals(rightButt))
            scrollMe(+70);
        else if (ev.getSource().equals(leftButt))
            scrollMe(-70);
        else if (ev.getSource() instanceof TouchButton) {
            Component[] comA = this.panelP.getComponents();
            SoundClip scCurr = null;
            for(int i=0;i<comA.length;i++)
                if(comA[i] instanceof SoundClip)
                    if(((SoundClip)comA[i]).getButtAdd().equals(ev.getSource()))
                        scCurr = ((SoundClip) comA[i]);
            
            SoundClip source = scCurr;
            this.panelP.remove(source);
            if(player.getActual().getFile().equals(source.getMp3File())){
                if(player.getPlaylist().size() <= 1){
                    try {
                        player.stop();
                        player = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(player != null){
                    try {
                        Track t = player.getActual();
                        player.next();
                        player.getPlaylist().remove(t);
                    } catch (LineUnavailableException e) {
                        e.printStackTrace();
                    } catch (UnsupportedAudioFileException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }  
            }else{
               java.util.Iterator<Track> it = player.getPlaylist().iterator();
               while(it.hasNext()){
                   Track iTT = it.next();
                   if(iTT.getFile().equals(source.getMp3File()))
                       player.getPlaylist().remove(iTT);
               }
            }
        }
       this.repaint();
       this.validate();
    }

    private void scrollMe(int scroll) {
        scrollP.getHorizontalScrollBar().setValue(
                scroll + scrollP.getHorizontalScrollBar().getValue());
    }

    public void addSong(SoundClip soundClip) {
        soundClip.setButtList(this, "Remove");
        panelP.add(soundClip);
        if (player == null)
            try {
                player = new Player(soundClip.getMp3File());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            }
        if (player != null) {
            try {
                player.insertTrack(new Track(soundClip.getMp3File()));
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(player != null){
            try {
//                if(player.playlis)
                    player.play();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            }}
    }

    /**
     * This method return the variable player.
     * @return the player
     */
    public Player getPlayer() {
        return player;
    }

    /**
     * This methods sets the variable player.
     * @param player the player to set
     */
    public void setPlayer(Player player) {
        this.player = player;
    }
    
    
}
